package com.funong.matrix.biz.base.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.funong.matrix.biz.base.entity.SysFileBusinessEntity;
import com.funong.matrix.biz.base.mapper.SysFileBusinessMapper;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.base.FunongBaseEntity;
import com.funong.matrix.core.enums.FileCategoryType;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.enums.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : NaNa
 * @since : 2023-05-26 18:22
 **/
@Aspect
@Dao
public class SysFileBusinessDao extends FunongBaseDao<SysFileBusinessMapper, SysFileBusinessEntity> {
    /**
     * 废弃文件关系信息，改状态
     *
     * @param bizId                来源ID
     *                             {@link FileCategoryType}
     * @param fileCategoryTypeList 业务文件类型
     * @param statusEnum           状态（驳回只更新文件状态为无效，isdeleted=0，未删除）
     */
    public List<SysFileBusinessEntity> getFileInfoByBizIdAndType(Integer bizId,
                                                                 List<String> fileCategoryTypeList,
                                                                 Integer statusEnum) {
        return this.list(Wrappers.<SysFileBusinessEntity>lambdaQuery()
                .eq(SysFileBusinessEntity::getBizId, bizId)
                .in(!CollectionUtils.isEmpty(fileCategoryTypeList), SysFileBusinessEntity::getBizCategory, fileCategoryTypeList)
                .eq(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .eq(null != statusEnum, SysFileBusinessEntity::getStatus, statusEnum)
        );
    }

    /**
     * 废弃文件关系信息，改状态为无效
     *
     * @param bizId        来源ID
     *                     {@link FileCategoryType}
     * @param categoryType 业务文件类型
     */
    public void dropFileRelation(Integer bizId, String categoryType, String memo) {
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(SysFileBusinessEntity::getBizCategory, categoryType)
                .eq(SysFileBusinessEntity::getBizId, bizId)
                .eq(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .eq(SysFileBusinessEntity::getStatus, StatusEnum.ENABLE.getValue())
                .set(SysFileBusinessEntity::getStatus, StatusEnum.DISABLE.getValue())
                .set(!StringUtils.isBlank(memo), SysFileBusinessEntity::getMemo, memo)
                .update();
    }

    /**
     * 删除文件关系信息
     *
     * @param bizId        来源ID
     *                     {@link FileCategoryType}
     * @param categoryType 业务文件类型
     */
    public void deleteFileRelation(Integer bizId, String categoryType) {
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(SysFileBusinessEntity::getBizId, bizId)
                .eq(SysFileBusinessEntity::getBizCategory, categoryType)
                .set(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.DELETED.getValue())
                .update();
    }

    public Integer queryFileCount(SysFileBusinessEntity sysFileBusinessEntity) {
        return count(Wrappers.<SysFileBusinessEntity>lambdaQuery()
                .eq(SysFileBusinessEntity::getBizModule, sysFileBusinessEntity.getBizModule())
                .eq(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .le(SysFileBusinessEntity::getId, sysFileBusinessEntity.getId())
        );
    }

    public List<SysFileBusinessEntity> queryFileBusinessList(SysFileBusinessEntity sysFileBusinessEntity) {
        return list(Wrappers.<SysFileBusinessEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getMemo()), SysFileBusinessEntity::getMemo, sysFileBusinessEntity.getMemo())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getFileUrl()), SysFileBusinessEntity::getFileUrl, sysFileBusinessEntity.getFileUrl())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getBizModule()), SysFileBusinessEntity::getBizModule, sysFileBusinessEntity.getBizModule())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getBizCategory()), SysFileBusinessEntity::getBizCategory, sysFileBusinessEntity.getBizCategory())
                .eq(sysFileBusinessEntity.getBizId() != null, SysFileBusinessEntity::getBizId, sysFileBusinessEntity.getBizId())
                .eq(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()
                ));


    }

    public IPage<SysFileBusinessEntity> queryContractFile(Page page, SysFileBusinessEntity sysFileBusinessEntity) {
        return page(page, Wrappers.<SysFileBusinessEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getMemo()), SysFileBusinessEntity::getMemo, sysFileBusinessEntity.getMemo())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getFileUrl()), SysFileBusinessEntity::getFileUrl, sysFileBusinessEntity.getFileUrl())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getBizModule()), SysFileBusinessEntity::getBizModule, sysFileBusinessEntity.getBizModule())
                .eq(StringUtils.isNotBlank(sysFileBusinessEntity.getBizCategory()), SysFileBusinessEntity::getBizCategory, sysFileBusinessEntity.getBizCategory())
                .eq(sysFileBusinessEntity.getBizId() != null, SysFileBusinessEntity::getBizId, sysFileBusinessEntity.getBizId())
                .eq(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()
                ));
    }

    /**
     * 删除
     *
     * @param bizModule
     * @param bizId
     * @param bizCategory
     */
    public void deleteFileBusiness(String bizModule, Integer bizId, String bizCategory) {
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(SysFileBusinessEntity::getBizModule, bizModule)
                .eq(SysFileBusinessEntity::getBizId, bizId)
                .eq(SysFileBusinessEntity::getBizCategory, bizCategory)
                .set(SysFileBusinessEntity::getIsDeleted, IsDeletedEnum.DELETED.getValue())
                .update();
    }

    /**
     * 查询
     *
     * @param bizModule
     * @param bizId
     * @param bizCategory
     * @return
     */
    public List<SysFileBusinessEntity> queryFileBusiness(String bizModule, Integer bizId, String bizCategory) {
        LambdaQueryWrapper<SysFileBusinessEntity> lqw = new LambdaQueryWrapper<SysFileBusinessEntity>()
                .eq(SysFileBusinessEntity::getBizModule, bizModule)
                .eq(SysFileBusinessEntity::getBizId, bizId)
                .eq(SysFileBusinessEntity::getBizCategory, bizCategory)
                .eq(FunongBaseEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue());
        return this.list(lqw);
    }
}
